package com.jee.ssm.modules.park.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSONObject;
import com.jee.ssm.model.Berth;
import com.jee.ssm.model.Park;
import com.jee.ssm.modules.park.services.ParkService;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.modules.ssm.controller.AdminBaseController;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.common.utils.UUIDFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* 停车场管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/park")
public class ParkController extends AdminBaseController<Park> {


    /**
     * 进入停车场添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("park:add")
    public String add(ModelMap model){

        model.put("longId", UUIDFactory.getStringId());
        return "manager/park/add";
    }


    /**
     * 进入停车场编辑页面
     * @param model 返回停车场的容器
     * @param id 停车场id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("park:edit")
    public String edit(ModelMap model, @RequestParam String id) {

        model.put("data",parkService.selectById(id));
        return "manager/park/edit";
    }


    /**
     * 停车场添加
     * @param park 带id的停车场对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("park:add")
    @AdminControllerLog(description = "添加停车场" )
    public Tip save(Park park)  {

        try {
            parkService.insert(park);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改停车场
     * @param park 带id的停车场对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("park:edit")
    @AdminControllerLog(description = "修改停车场" )
    public Tip update(Park park) {

        try {
            parkService.updateById(park);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除停车场
     * @param id 停车场id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("park:delete")
    @AdminControllerLog(description = "删除停车场" )
    public Tip delete(@RequestParam String id) {

        try {
            parkService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 停车场id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("park:delete")
    @AdminControllerLog(description = "批量删除停车场" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            parkService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找停车场
     * @param id 停车场id
     * @return 停车场对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("park:list")
    public Park find(@RequestParam String id) {

        return parkService.selectById(id);
    }


    /**
     * 获取停车场列表 获取全部 不分页
     * @param request 请求参数
     * @return 停车场列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("park:list")
    public String all(HttpServletRequest request,ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取停车场列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 停车场列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("park:list")
    public String list(HttpServletRequest request,ModelMap modelMap,Integer page,Integer size) {

        modelMap.put("pageInfo",parkService.list(new ParamMap(request),page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/park/list";
    }

    /**
     * 服务器停车场列表
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/serverParkList")
    public String serverParkList(HttpServletRequest request,ModelMap modelMap){
        ParamMap paramMap = new ParamMap(request);
        List<Park> parkList = new ArrayList<>();
        try {
            Props props = new Props("config/project.properties");
            String serverUrl = props.getProperty("server.url");
            String method = props.getProperty("getRoadsideParking");
            String result= HttpUtil.get(serverUrl + method, paramMap);
            parkList = JSONObject.parseArray(result, Park.class);
        }catch (Exception e){
            //e.printStackTrace();
            System.out.println("服务器未链接！");
        }
        modelMap.put("pageInfo",parkList);
        modelMap.putAll(paramMap);
        return "manager/park/listWithOutPage";
    }

    /**
     * 服务器车位列表
     * @param parkId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "checkBerthList")
    public String serverBerthList(String parkId,ModelMap modelMap){
        List<Berth> berthList = new ArrayList<>();
        try {
            Props props = new Props("config/project.properties");
            String serverUrl = props.getProperty("server.url");
            String method = props.getProperty("getAllBerthByParkId");
            String result= HttpRequest.get(serverUrl + method).form("parkId",parkId).execute().body();
            berthList = JSONObject.parseArray(result, Berth.class);
        }catch (Exception e){
            //e.printStackTrace();
            System.out.println("服务器未链接！");
        }
        modelMap.put("pageInfo",berthList);
        modelMap.put("parkId",parkId);
        return "manager/park/berthList";

    }



    //---------------------------- property -------------------------------

    @Resource
    private ParkService parkService;

}
